﻿
# DevScope BI PowerShell Samples
# 
# http://devscopebisamples.codeplex.com/
# http://www.devscope.net

write-host "---MailReports---"

Import-Module .\DevScope.BI.Samples.PowerShell.psm1 -Force

. .\execconfig.ps1

$reportViewsSchema="mail"

Invoke-OLEDBCommand -connectionString $connStr `
	-sql "select Name from sys.views where SCHEMA_NAME(schema_id) = '$reportViewsSchema'" |%  {
	
	# for each view found
	$viewName = $reportViewsSchema+"."+$_.Name
	
	$data = Invoke-OLEDBCommand -connectionString $connStr -sql "select * from $viewName"

	$alertCount=($data | ? {$_.Alert -eq 1}).COunt
	write-host "Alerts found: $alertCount"

	# send mail only if alert column is present and its 1
	if ($alertCount -gt 0) 
		{
		$html = Out-Email -subject "Alerts in $viewName : $alertCount" -inputObject $data -to $mailto -from $mailfrom -smtpServer $mailsmtp
		}

}

write-host "Done!"


